<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class BaseModel extends Model
{
    protected $connection = 'api';

    protected function getOSSUrl()
    {
        return config('kkaudio.oss_url');
    }

    protected function defaultHeadImg()
    {
        return '/static/base/images/no_head_img.png';
    }

    public function batchInsert(string $table, array $data)
    {
        return DB::table($table)->insert($data);
    }

    //返回数组
    public function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }

    /**
     * 根据条件返回列表
     * @param $condition
     * @param $fields
     * @return mixed
     */
    public function getListByCondition($condition, $fields, $orderBy = [], $groupBy = [])
    {
        $query = DB::connection($this->connection)->table($this->table)
            ->where($condition)
            ->select($fields);
        if($groupBy){
            $query->groupBy($groupBy);
        }

        if(!empty($orderBy['field']) && !empty($orderBy['sort'])){
            $query->orderBy($orderBy['field'], $orderBy['sort']);
        }

        $result = $query->get();
        return $this->objToArray($result);
    }
        /**
     * 根据条件返回列表
     * @param $condition
     * @param $fields
     * @return mixed
     */
    public function getListByConditionPage($condition, $fields, $limit, $orderBy = [], $groupBy = [])
    {
        $query = DB::connection($this->connection)->table($this->table)
            ->where($condition)
            ->select($fields);
        if($groupBy){
            $query->groupBy($groupBy);
        }

        if(!empty($orderBy['field']) && !empty($orderBy['sort'])){
            $query->orderBy($orderBy['field'], $orderBy['sort']);
        }
        $result = $query->paginate($limit);
        
        return $this->objToArray($result);
    }
    /**
     * 根据条件返回一条数据
     * @param $condition
     * @param $fields
     * @return mixed
     */
    public function getOneByCondition($condition, $fields)
    {
        $result = DB::connection($this->connection)->table($this->table)
            ->where($condition)
            ->select($fields)
            ->first();
        return $this->objToArray($result);
    }

    /**
     * 根据条件更新
     * @param $condition
     * @param $data
     * @return mixed
     */
    public function updateByCondition($condition, $data)
    {
        $result = DB::connection($this->connection)->table($this->table)
            ->where($condition)
            ->update($data);
        return $result;
    }

    /**
     * 写入指定数据库连接表
     * @param $condition
     * @param $data
     * @return mixed
     */
    public function insertByConnection($data)
    {
        $result = DB::connection($this->connection)->table($this->table)->insert($data);
        
        return $result;
    }
}
